﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using CharitiesLib;
namespace Charities
{
    public partial class SupplyTypes : Form
    {
        int userId = -1;
        Form fo = null;
        public SupplyTypes(int id,Form m)
        {
            fo = m;
            this.MdiParent = m;
            userId = id;
            InitializeComponent();
        }
        ModelContainer ctx = new ModelContainer();
        public bool is_loaded = false;
        private void SupplyTypes_Load(object sender, EventArgs e)
        {
            is_loaded = true;
            // add this form to window list
            ((Forms.FormMDI)fo).addFormToWindowsList(this);
            User user = ctx.Users.Single(u => u.Id == userId);
            foreach (var Item in user.Role.Permissions)
            {
                switch (Item.Name)
                {
                        
                    case "SUPPLY_TYPES_DELETE":
                        btn_Delete.Visible = true;
                        break;
                    default:
                        break;
                }
            }

            supplyTypeBindingSource.DataSource = ctx.SupplyTypes.ToList();
            SupplyTypesdataGridView.DataSource = supplyTypeBindingSource;
            
        }
        private void SupplyTypesdataGridView_RowValidated(object sender, DataGridViewCellEventArgs e)
        {
            if (SupplyTypesdataGridView.Rows[e.RowIndex].Cells[1].Value != null && SupplyTypesdataGridView.SelectedRows.Count==1)
            {
                string name = SupplyTypesdataGridView.Rows[e.RowIndex].Cells[1].Value.ToString().Trim();
                if (ctx.SupplyTypes.Where(c => c.Name == name).Count() > 0)
                {
                    int id;
                    if (int.TryParse(SupplyTypesdataGridView.Rows[e.RowIndex].Cells[0].Value.ToString(), out id) && id > 0)
                    {
                        SupplyType ins = ctx.SupplyTypes.Single(c => c.Id == id);
                        ins.Name = name;
                        ins.UpdateDate = DateTime.Now;
                        ins.UpdateUId = userId;
                    }
                }
                else
                {
                    if (SupplyTypesdataGridView.Rows[e.RowIndex].Cells[1].Value != null)
                    {
                        SupplyType i = new SupplyType();
                        i.Name = name;
                        i.InsertDate = DateTime.Now;
                        i.InsertUId = userId;
                        ctx.SupplyTypes.Add(i);
                    }
                }
                preditName = name;
            }
            ctx.SaveChanges();

        }

        private void btn_Add_Click(object sender, EventArgs e)
        {
            string text = txb_name.Text;
            text.Trim();
            try
            {
               
                SupplyType s = ctx.SupplyTypes.Single(c => c.Name == text);
            }
            catch (Exception)
            {
                if (text != "")
                {

                    SupplyType s = new SupplyType();
                    s.Name = text;
                    s.InsertDate = DateTime.Now;
                    s.InsertUId = userId;
                    ctx.SupplyTypes.Add(s);
                    ctx.SaveChanges();
                    supplyTypeBindingSource.DataSource = (from s1 in ctx.SupplyTypes select s1).ToList();
                    SupplyTypesdataGridView.DataSource = supplyTypeBindingSource;
                    txb_name.Text = "";
                }
                else
                {
                    MessageBox.Show("يرجى اضافة اسم صالح");
                }
            }
          
        }

        private void btn_dispose_Click(object sender, EventArgs e)
        {
            this.Close();
        }

        private void btn_Delete_Click(object sender, EventArgs e)
        {
            DialogResult result = MessageBox.Show(" هل تريد بالتأكيد حذف المعونات المختارة سيؤدي هذا الحذف لحذف المعونات المنتسبة لهذا الصنف","تأكيد الحذف",MessageBoxButtons.YesNo,MessageBoxIcon.Warning);
            if (result == DialogResult.Yes)
            {
                for (int i = 0; i < SupplyTypesdataGridView.SelectedRows.Count; i++)
                {
                    if (!SupplyTypesdataGridView.SelectedRows[i].IsNewRow || SupplyTypesdataGridView.SelectedRows[i].Cells[0].Value!=null)
                    {
                        int id = (int)SupplyTypesdataGridView.SelectedRows[i].Cells[0].Value;
                        if (ctx.SupplyTypes.Where(c => c.Id == id).Count() > 0)
                        {
                            if (ctx.SupplyTypes.Single(c => c.Id == id).Supplies.Count() > 0)
                            {
                                MessageBox.Show("لايمكن حذف صنف معونة " + SupplyTypesdataGridView.SelectedRows[i].Cells[1].Value.ToString() + "  لكونه يجتوي على معونات  ");
                            }
                            else
                            {
                                SupplyType s = ctx.SupplyTypes.Single(c => c.Id == id);
                                ctx.SupplyTypes.Remove(s);
                                ctx.SaveChanges();
                            }
                        }
                    }
                }
            }
                supplyTypeBindingSource.DataSource = (from s1 in ctx.SupplyTypes select s1).ToList();
                SupplyTypesdataGridView.DataSource = supplyTypeBindingSource;
        }

        private void txb_name_Click(object sender, EventArgs e)
        {
            if (txb_name.Text == String.Empty)
            {
                btn_Edit.Visible = false;
                btn_Add.Visible = true;
                btn_Delete.Enabled = false;
            }
        }

        private void btn_Edit_Click(object sender, EventArgs e)
        {
            try
            {
                SupplyType s = ctx.SupplyTypes.Single(c => c.Name == preditName);
                s.Name = txb_name.Text;
                s.UpdateDate = DateTime.Now;
                s.UpdateUId = userId;
                ctx.SaveChanges();
                txb_name.Text = String.Empty;
                supplyTypeBindingSource.DataSource = (from c in ctx.SupplyTypes select c).ToList();
                SupplyTypesdataGridView.DataSource = supplyTypeBindingSource;

            }
            catch (Exception ex)
            {

                MessageBox.Show(ex.Message);
            }
        }
        string preditName;
        private void SupplyTypesdataGridView_CellDoubleClick(object sender, DataGridViewCellEventArgs e)
        {

            if (SupplyTypesdataGridView.Rows[e.RowIndex].Cells[1].Value != null)
            {
                string name = SupplyTypesdataGridView.Rows[e.RowIndex].Cells[1].Value.ToString().Trim();
                txb_name.Text = name;
                btn_Add.Visible = false;
                btn_Edit.Visible = true;
                btn_Delete.Enabled = true;
                preditName = name;
                txb_name.Focus();
            }

        }

        private void SupplyTypes_FormClosed(object sender, FormClosedEventArgs e)
        {
            is_loaded = false;
            // remove this form from windows list
            ((Forms.FormMDI)fo).removeFormFromWindowsList(this);
        }

        private void SupplyTypes_Activated(object sender, EventArgs e)
        {
            // activate this form and add it windows list as activate one
            ((Forms.FormMDI)this.MdiParent).activateWindow(this);
        }

        private void SupplyTypesdataGridView_MultiSelectChanged(object sender, EventArgs e)
        {
            btn_Delete.Enabled = true;
        }

    }
}
